unit UntMatchingData;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DBGridEhGrouping, GridsEh, DBGridEh, StdCtrls, DB, DBClient;

type
  TFrmMatching = class(TForm)
    dbgrdhMatching: TDBGridEh;
    btnOK: TButton;
    btnCancel: TButton;
    dsMatching: TDataSource;
    cdsMatching: TClientDataSet;
    procedure btnCancelClick(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
    procedure cdsMatchingBeforeInsert(DataSet: TDataSet);
  private
    FcdsExcel: TClientDataSet;
    FcdsToDB: TClientDataSet;
    { Private declarations }
  public
    procedure inital(cdsExcel: TClientDataSet;cdsToDB: TClientDataSet);
    { Public declarations }
  end;

implementation

{$R *.dfm}

procedure TFrmMatching.btnCancelClick(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

procedure TFrmMatching.btnOKClick(Sender: TObject);
var lfield : TField;
  excelFileName : String;
  lafs : Array of integer;
  lindex :integer;
begin
  SetLength(lafs,cdsMatching.RecordCount);
  dbgrdhMatching.Enabled := false;
  cdsMatching.First;
  lindex :=0;
  while not cdsMatching.eof do
  begin
    lafs[lindex]:=-1;
    excelFileName := cdsMatching.FieldByName('ExcelFields').AsString;
    if excelFileName<>'' then
    begin
      lfield := FcdsExcel.FieldByName(excelFileName);
      if Assigned(lfield) then
      begin
        lafs[lindex]:=lfield.Index;
      end;
    end;
    inc(lindex);
    cdsMatching.Next;
  end;
  dbgrdhMatching.Enabled := true;
  try
    FcdsToDB.DisableControls;
    FcdsExcel.DisableControls;

    FcdsToDB.First;
    while FcdsToDB.RecordCount>0 do
    begin
      FcdsToDB.Delete;
    end;

    FcdsExcel.First;
    while not FcdsExcel.eof do
    begin
      FcdsToDB.Append;
      lindex :=0;
      while lindex< FcdsToDB.FieldCount do
      begin
        if lafs[lindex]>=0 then
        begin
          FcdsToDB.Fields[lindex].AsString:= FcdsExcel.Fields[lafs[lindex]].AsString;
        end;
        inc(lindex);
      end;
      FcdsToDB.Post;
      FcdsExcel.Next;
    end;
    
  finally
    FcdsToDB.EnableControls;
    FcdsExcel.EnableControls;
  end;
end;

procedure TFrmMatching.cdsMatchingBeforeInsert(DataSet: TDataSet);
begin
  cdsMatching.Cancel;
end;

procedure TFrmMatching.inital(cdsExcel: TClientDataSet;cdsToDB: TClientDataSet);
var lindex :integer;
  ldbcol : TColumnEh;
begin
  FcdsExcel := cdsExcel;
  FcdsToDB := cdsToDB;
  cdsMatching.Close;
  cdsMatching.BeforeInsert := nil;
  cdsMatching.CreateDataSet;
  lindex :=0 ;
  while lindex< FcdsToDB.FieldCount do
  begin
    cdsMatching.Append;
    cdsMatching.FieldByName('DBFields').AsString := FcdsToDB.Fields[lindex].FieldName;
    cdsMatching.Post;
    inc(lindex);
  end;
  lindex :=0 ;
  dbgrdhMatching.Columns[0].ReadOnly := true;
  ldbcol := dbgrdhMatching.Columns[1];
  ldbcol.PickList.Clear;
  while lindex< FcdsExcel.FieldCount do
  begin
    ldbcol.PickList.Add(FcdsExcel.Fields[lindex].FieldName);
    inc(lindex);
  end;
  ldbcol.AutoDropDown := True;
  ldbcol.AlwaysShowEditButton := true;
  cdsMatching.BeforeInsert := cdsMatchingBeforeInsert;
end;

end.
